#include <bits/stdc++.h>
#define Fst first
#define Snd second
#define MP make_pair
using namespace std;
int n,m,tac,K=0;
int a[140000],ai[140000];
pair<int,int> que[100010];
long long ans[100010];
string s[30];
char str[140000];
int X[300][4];
int mrk[560000];
int msk[560000];
int tur[560000];
long long sum[560000];
int mxt[140000];
inline void MERG(int t,int x,int y,int z,int tr)
{
    if (z) swap(x,y);
    int bg=(1<<(K+1))-(1<<tr);
    int r=(x&bg); if ((x&bg)!=x) r|=y;
    msk[t]=r; mrk[t]=(((x&bg)==x) ? 1 : 0);
    if (!mrk[t]) sum[t]=sum[t*2]+sum[t*2+1];
    else sum[t]=(!z ? sum[t*2] : sum[t*2+1]);
    //cerr <<t<<" "<<x<<" "<<y<<" "<<z<<" "<<tr<<" MERG\n";
}
inline void BUILD(int t,int l,int r)
{
    if (l==r)
    {
        tur[t]=0,sum[t]=l+1,mrk[t]=0;
        msk[t]=(1<<(K+1))-1;
        return;
    }
    int mid=(l+r)>>1;
    BUILD(t*2,l,mid),BUILD(t*2+1,mid+1,r);
    tur[t]=tur[t*2]+1;
    MERG(t,msk[t*2],msk[t*2+1],s[tur[t]-1][l>>tur[t]]-'0',tur[t]);
}
inline void GETHV(int t,int l,int r,int vl)
{
    if (l==r)
    {
        mxt[l]=vl;
        return;
    }
    int mid=(l+r)>>1; int dr=s[tur[t]-1][l>>tur[t]]-'0';
    GETHV(t*2,l,mid,max(vl,(!dr ? tur[t] : 0)));
    GETHV(t*2+1,mid+1,r,max(vl,(dr ? tur[t] : 0)));
}
inline void CHG(int t,int l,int r,int ps,int vl,int hv)
{
    if (l==r)
    {
        msk[t]=(1<<vl); sum[t]=(hv ? l+1 : 0);
        return;
    }
    int mid=(l+r)>>1;
    if (ps<=mid) CHG(t*2,l,mid,ps,vl,hv);
    else CHG(t*2+1,mid+1,r,ps,vl,hv);
    MERG(t,msk[t*2],msk[t*2+1],s[tur[t]-1][l>>tur[t]]-'0',tur[t]);
    //cerr <<t<<" "<<l<<" "<<r<<" "<<sum[t]<<" "<<mrk[t]<<" CHG\n";
}
inline long long SUM(int t,int l,int r,int li,int ri)
{
    if (r<li || ri<l) return 0;
    if (li<=l && r<=ri) return sum[t];
    int mid=(l+r)>>1;
    return SUM(t*2,l,mid,li,ri)+SUM(t*2+1,mid+1,r,li,ri);
}
inline void Main()
{
    BUILD(1,0,(1<<K)-1); GETHV(1,0,(1<<K)-1,0);
    int it=0; long long res=0;
    for (int i=0;i<m;i++)
    {
        int ln=que[i].Fst;
        while (it<=ln)
        {
            CHG(1,0,(1<<K)-1,it,a[it],(a[it]>=mxt[it] ? 1 : 0));
            //cerr <<it+1<<" ++a\n";
            it++;
        }
        int Ki=0; while ((1<<Ki)<(ln+1)) Ki++;
        ans[que[i].Snd]=SUM(1,0,(1<<K)-1,0,(1<<Ki)-1);
        //cerr <<que[i].Snd+1<<" que get\n";
    }
    for (int i=0;i<m;i++) res^=(ans[i]*(i+1));
    printf("%lld\n",res);
}
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (int i=0;i<n;i++) scanf("%d",&ai[i]);
    for (int i=0;i<m;i++) scanf("%d",&que[i].Fst),que[i].Fst--,que[i].Snd=i;
    sort(que,que+m);
    while ((1<<K)<n) K++;
    for (int i=0;i<K;i++)
    {
        scanf("%s",str);
        s[i]=str;
    }
    scanf("%d",&tac);
    for (int i=0;i<tac;i++)
        for (int j=0;j<4;j++) scanf("%d",&X[i][j]);
    for (int i=0;i<tac;i++)
    {
        for (int j=0;j<n;j++) a[j]=ai[j]^X[i][(j+1)%4];
        for (int j=0;j<n;j++) a[j]=min(a[j],K);
        //cerr <<i<<" ---------------------------------------\n";
        //for (int j=0;j<n;j++) cerr <<a[j]<<" ";
        //cerr <<"a\n";
        Main();
    }
    return 0;
}